﻿using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using DCT.Model;
using Microsoft.Win32;
using System.IO;

namespace DCT.ViewModel
{
    public class MainWindowViewModel : ViewModelBase
    {
        #region Constructor

        public MainWindowViewModel()
        {
            _deviceMapRepository = new DeviceMapViewModelRepository();
        }

        #endregion // Constructor

        #region Fields

        DeviceMapViewModelRepository _deviceMapRepository;
        List<DeviceBaseViewModel> _dragDevices;
        EditModeState _editMode;

        #endregion // Fields

        #region Properties


        //WorkspaceViewModel _activeWorkspace;
        //public WorkspaceViewModel ActiveWorkspace
        //{
        //    get
        //    {
        //        return _activeWorkspace;
        //    }
        //    set
        //    {
        //        if (value != _activeWorkspace)
        //        {
        //            _activeWorkspace = value;

        //            base.OnPropertyChanged("ActiveWorkspace");
        //        }
        //    }
        //}

        public DeviceMapViewModelRepository DeviceMapRepository
        {
            get
            {
                return _deviceMapRepository;
            }
        }

        public List<DeviceBaseViewModel> DragDevices
        {
            get
            {
                if (_dragDevices == null)
                {
                    _dragDevices = new List<DeviceBaseViewModel>(CreateDragDevices());
                }

                return _dragDevices;
            }
        }

        public string CurrentProjectFileName
        {
            get
            {
                return DataAccess.Instance.CurrentXMLFileName;
            }
        }

        public EditModeState EditMode
        {
            get
            {
                return _editMode;
            }
            set
            {
                if (value != _editMode)
                {
                    _editMode = value;

                    base.OnPropertyChanged("EditMode");
                }
            }
        }

        #endregion // Properties

        #region Commands

        #region New Command

        public void New_Execute()
        {
            _deviceMapRepository.New();
            base.OnPropertyChanged("CurrentProjectFileName");
        }

        RelayCommand _newCommand;
        public ICommand NewCommand
        {
            get
            {
                if (_newCommand == null)
                    _newCommand = new RelayCommand(param => this.New_Execute());

                return _newCommand;
            }
        }

        #endregion

        #region Open Command

        public void Open_Execute()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "XML Files (*.xml)|*.xml|DB Files (*.s3db)|*.s3db";

            if (openFileDialog.ShowDialog() == true)
            {
                _deviceMapRepository.LoadProjectFile(openFileDialog.FileName);
                base.OnPropertyChanged("CurrentProjectFileName");
            }
        }

        RelayCommand _openCommand;
        public ICommand OpenCommand
        {
            get
            {
                if (_openCommand == null)
                    _openCommand = new RelayCommand(param => this.Open_Execute());

                return _openCommand;
            }
        }

        #endregion

        #region Save Command

        public bool Save_Can()
        {
            return this._deviceMapRepository.DeviceMapViewModels.Where(v => v.IsChanged).Any();
        }

        public void Save_Execute(object fileName)
        {
            string fileNameString = fileName as string;
            if (!string.IsNullOrEmpty(fileNameString) && File.Exists(fileNameString))
            {
                _deviceMapRepository.SaveProjectFile(fileNameString);
                base.OnPropertyChanged("CurrentProjectFileName");

                foreach (DeviceMapViewModel deviceMapViewModel in this._deviceMapRepository.DeviceMapViewModels)
                {
                    deviceMapViewModel.IsChanged = false;
                    deviceMapViewModel.UndoRedo.SetChangeState();
                }
            }
            else
            {
                this.SaveAs_Execute();
            }
        }

        RelayCommand _saveCommand;
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                    _saveCommand = new RelayCommand(this.Save_Execute,
                                                    param => this.Save_Can());

                return _saveCommand;
            }
        }

        #endregion

        #region SaveAs Command

        public void SaveAs_Execute()
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "XML Files (*.xml)|*.xml|DB Files (*.s3db)|*.s3db";

            if (saveFileDialog.ShowDialog() == true)
            {
                this.Save_Execute(saveFileDialog.FileName);
            }
        }

        RelayCommand _saveAsCommand;
        public ICommand SaveAsCommand
        {
            get
            {
                if (_saveAsCommand == null)
                    _saveAsCommand = new RelayCommand(param => this.SaveAs_Execute());

                return _saveAsCommand;
            }
        }

        #endregion

        #region Mode Command

        public void Mode_Execute(object mode)
        {
            DeviceMapViewModel deviceMapViewModel = WorkspaceManager.Instance.ActiveWorkspace as DeviceMapViewModel;

            if (deviceMapViewModel != null)
            {
                deviceMapViewModel.EditMode = this.EditMode = (EditModeState)mode;
            }
        }

        public bool Mode_Can(object mode)
        {
            DeviceMapViewModel deviceMapViewModel = WorkspaceManager.Instance.ActiveWorkspace as DeviceMapViewModel;

            if (deviceMapViewModel != null)
            {
                if ((EditModeState)mode != EditModeState.Move)
                {
                    return deviceMapViewModel.IsMainMap;
                }

                return true;
            }

            return false;
        }

        RelayCommand _modeCommand;
        public ICommand ModeCommand
        {
            get
            {
                if (_modeCommand == null)
                    _modeCommand = new RelayCommand(this.Mode_Execute,
                                                    this.Mode_Can);

                return _modeCommand;
            }
        }

        #endregion

        #endregion // Commands

        #region Methods

        public void Init()
        {
            this._deviceMapRepository.ShowProject();
            base.OnPropertyChanged("CurrentProjectFileName");
        }

        List<DeviceBaseViewModel> CreateDragDevices()
        {
            return new List<DeviceBaseViewModel>
            {
                new SignalViewModel(
                    new SignalModel
                    {
                        DeviceType = "Signal"
                    },
                    new SignalGraphModel
                    {
                        DeviceHeight = 1600,
                        DeviceWidth = 2500
                    }
                    ),

                new AxleViewModel(
                    new AxleModel
                    {
                        DeviceType = "Axle"
                    },
                    new AxleGraphModel
                    {
                        DeviceHeight = 900,
                        DeviceWidth = 1900
                    }
                    ),

                new PlatformViewModel(
                    new PlatformModel
                    {
                        DeviceType = "Platform"
                    },
                    new PlatformGraphModel
                    {
                        DeviceHeight = 1800,
                        DeviceWidth = 10800
                    }
                    ),
            };
        }

        #endregion // Methods
    }
}
